package cn.kgc.controller;

import cn.kgc.entity.User;
import cn.kgc.service.UserService;
import cn.kgc.util.Common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;

/**
 * @author Rock
 * @create 2020-03-03 20:23
 */
@Controller
public class LoginController {
    @Autowired
    private UserService service;
    /**
     * 登录方法需要把用户放到session域中传回登录页面
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(User user, Model model, HttpSession session){
        User loginUser = service.login(user);
        if (null != loginUser){
            if (loginUser.getStatus()==1){
                //登录成功！为方便后期操作，key设定一个常量
                session.setAttribute(Common.LOGIN_USER,loginUser);
                return "welcome";
            }
            model.addAttribute("msg","您没有系统全限，请联系管理员！");
            return "login";
        }
        //登录失败！将失败提示传回给登录页面
        model.addAttribute("msg","用户编码或密码错误！请重新输入");
        return "login";
    }

    /**
     * 登录超时时返回登录页面
     * @return
     */
    @RequestMapping(value = "/toLogin",method = RequestMethod.GET)
    public String toLogin(){
        return "login";
    }
    /**
     * 到顶部页面
     */
    @RequestMapping(value="/sys/toTop")
    public String totop(){
        return "top";
    }
    /**
     * 到左侧页面
     */
    @RequestMapping("/sys/toLeft")
    public String toLeft(){
        return "left";
    }
    /**
     * 到底部页面
     */
    @RequestMapping("/sys/toFoot")
    public String toFoot(){
        return "foot";
    }
    /**
     * 到中间页面
     */
    @RequestMapping("/sys/toMain")
    public String toMain(){
        return "main";
    }
}
